package com.xudow.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.xudow.app.R;
import com.xudow.app.ui.widget.WeekChooseWheelView;
import com.xudow.app.util.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 周选择弹出窗口
 *
 * @author lukecheng
 */
public class WeekChoosePopupWindow extends PopupWindow implements View.OnClickListener {

    public interface OnOkClickListner {
        void onOkClick(int year, int month, int week);
    }

    private OnOkClickListner okClickListner;

    public void setOkClickListner(OnOkClickListner okClickListner) {
        this.okClickListner = okClickListner;
    }

    private Context context;

    private View contentView;
    private WeekChooseWheelView yearsView, monthView, weekView;
    private Button okBtn;

    private List<String> years;
    private List<String> months;
    private List<String> weeks;

    private int curYear; //当前年
    private int curMonth; //当前月
    private int curWeek; //当前周

    public WeekChoosePopupWindow(final Context context, int y, int m, final int w) {
        super(context);

        this.context = context;

        this.curYear = y;
        this.curMonth = m;
        this.curWeek = w;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.layout_week_choose_pupup_window, null);

        setContentView(contentView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setAnimationStyle(R.style.PopupAnimation);

        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        setBackgroundDrawable(dw);

        initRecentYears();  // 初始化年
        yearsView = (WeekChooseWheelView) contentView.findViewById(R.id.year);
        yearsView.setItems(years);
        yearsView.setOffset(1);

        for (int i = 0; i < years.size(); i++) {
            if (years.get(i).equals(curYear + "年")) {
                yearsView.setSeletion(i);
                break;
            }
        }

        yearsView.setOnWheelViewListener(new WeekChooseWheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);

                updateWeek();
            }
        });

        initMonths();
        monthView = (WeekChooseWheelView) contentView.findViewById(R.id.month);
        monthView.setItems(months);

        for (int i = 0; i < months.size(); i++) {
            if (months.get(i).equals(curMonth + "月")) {
                monthView.setSeletion(i);
                break;
            }
        }

        monthView.setOnWheelViewListener(new WeekChooseWheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);

                updateWeek();
            }
        });

        initWeeks();
        weekView = (WeekChooseWheelView) contentView.findViewById(R.id.week);
        weekView.setItems(weeks);

        for (int i = 0; i < weeks.size(); i++) {
            if (weeks.get(i).equals(curWeek + "周")) {
                weekView.setSeletion(i);
                break;
            }
        }

        weekView.setOnWheelViewListener(new WeekChooseWheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);

                String week = item.replace("周", "");
                curWeek = Integer.parseInt(week);
            }
        });

        okBtn = (Button) contentView.findViewById(R.id.ok);
        okBtn.setOnClickListener(this);
    }

    private void updateWeek() {
        String yearStr = yearsView.getSeletedItem();
        String monthStr = monthView.getSeletedItem();

        String year = yearStr.replace("年", "");
        String month = monthStr.replace("月", "");

        curYear = Integer.parseInt(year);
        curMonth = Integer.parseInt(month);

        initWeeks();

        weekView.setItems(weeks);
        weekView.setSeletion(0);
    }

    /**
     * 初始化年
     */
    public void initRecentYears() {
        years = new ArrayList<String>(10);

        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR); // 得到当前年

        for (int i = currentYear - 5; i <= currentYear + 5; i++) {  // 往list里面循环添加当前年的前后五年
            years.add("" + i + "年");
        }
    }

    /**
     * 初始化月
     */
    public void initMonths() {
        months = new ArrayList<String>();

        for (int i = 1; i <= 12; i++) {
            months.add("" + i + "月");
        }
    }

    /**
     * 初始化周
     */
    public void initWeeks() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, curYear);
        calendar.set(Calendar.MONTH, curMonth - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        int weekSize = TimeUtils.getMonthWeeks(calendar.getTime());

        weeks = new ArrayList<String>();
        for (int i = 1; i <= weekSize; i++) {
            weeks.add(i + "周");
        }
    }

    @Override
    public void onClick(View v) {
        if (null != okClickListner) {
            String yearStr = yearsView.getSeletedItem();
            String monthStr = monthView.getSeletedItem();
            String weekStr = weekView.getSeletedItem();

            String year = yearStr.replace("年", "");
            String month = monthStr.replace("月", "");
            String week = weekStr.replace("周", "");

            curYear = Integer.parseInt(year);
            curMonth = Integer.parseInt(month);
            curWeek = Integer.parseInt(week);

            okClickListner.onOkClick(curYear, curMonth, curWeek);
        }
    }
}
